package com.ldj.base;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * All rights Reserved, Designed By 翼虎能源
 * Copyright:    Copyright(C) 2015-2015
 * Company       北京翼虎能源科技有限公司
 *
 * @author 吕东杰
 * @version 1.0
 * @date 2016/5/26 15:51
 * @Description Demo
 */
public class Page<T> {
    public static final String ASC = "asc";
    public static final String DESC = "desc";
    protected int pageNumber = 1;
    protected int pageSize = -1;
    protected String orderBy = null;
    protected String order = null;
    protected boolean autoCount = true;
    protected List<T> result = new ArrayList();
    protected long totalCount = -1L;
    protected long pageCount = -1L;

    public Page() {
    }

    public Page(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNumber() {
        return this.pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
        if (pageNumber < 1) {
            this.pageNumber = 1;
        }

    }

    public Page<T> pageNumber(int pageNuber) {
        this.setPageNumber(pageNuber);
        return this;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Page<T> pageSize(int thePageSize) {
        this.setPageSize(thePageSize);
        return this;
    }

    public int getFirst() {
        return (this.pageNumber - 1) * this.pageSize + 1;
    }

    public String getOrderBy() {
        return this.orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public Page<T> orderBy(String theOrderBy) {
        this.setOrderBy(theOrderBy);
        return this;
    }

    public String getOrder() {
        return this.order;
    }

    public void setOrder(String order) {
        String lowcaseOrder = StringUtils.lowerCase(order);
        String[] orders = StringUtils.split(lowcaseOrder, ',');
        String[] arr$ = orders;
        int len$ = orders.length;

        for (int i$ = 0; i$ < len$; ++i$) {
            String orderStr = arr$[i$];
            if (!StringUtils.equals("desc", orderStr) && !StringUtils.equals("asc", orderStr)) {
                throw new IllegalArgumentException("排序方向" + orderStr + "不是合法值");
            }
        }

        this.order = lowcaseOrder;
    }

    public Page<T> order(String theOrder) {
        this.setOrder(theOrder);
        return this;
    }

    public boolean isOrderBySetted() {
        return StringUtils.isNotBlank(this.orderBy) && StringUtils.isNotBlank(this.order);
    }

    public boolean isAutoCount() {
        return this.autoCount;
    }

    public void setAutoCount(boolean autoCount) {
        this.autoCount = autoCount;
    }

    public Page<T> autoCount(boolean theAutoCount) {
        this.setAutoCount(theAutoCount);
        return this;
    }

    public List<T> getResult() {
        return this.result;
    }

    public void setResult(List<T> result) {
        this.result = result;
    }

    public long getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public long getTotalPages() {
        if (this.totalCount < 0L) {
            return -1L;
        } else {
            long count = this.totalCount / (long) this.pageSize;
            if (this.totalCount % (long) this.pageSize > 0L) {
                ++count;
            }

            return count;
        }
    }

    public boolean isHasNext() {
        return (long) (this.pageNumber + 1) <= this.getTotalPages();
    }

    public int getNextPage() {
        return this.isHasNext() ? this.pageNumber + 1 : this.pageNumber;
    }

    public boolean isHasPre() {
        return this.pageNumber - 1 >= 1;
    }

    public int getPrePage() {
        return this.isHasPre() ? this.pageNumber - 1 : this.pageNumber;
    }

    public long getPageCount() {
        if (this.totalCount < 0L) {
            return -1L;
        } else {
            long count = this.totalCount / (long) this.pageSize;
            if (this.totalCount % (long) this.pageSize > 0L) {
                ++count;
            }

            return this.pageCount < 0L ? count : this.pageCount;
        }
    }

    public void setPageCount(long pageCount) {
        this.pageCount = pageCount;
    }
}
